<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2019/12/23
// +----------------------------------------------------------------------

namespace app\common\services\wechattemplate;

use app\common\facade\Arr;
use app\common\services\WechatTemplateService;
use think\Db;

/**
 * 二手市场 -- 微信模版消息服务类
 * Class MarketTemplateService
 * @package app\common\services\wechattemplate
 * @author feraner<feraner@163.com>
 * @date 2019/12/23
 */
class MarketTemplateService extends WechatTemplateService
{


    public function __construct()
    {
        parent::__construct();
    }


    /**
     * 发送给代理商 -- 审核二手商品 通知
     * @param $goodsInfo
     *
     * @return bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendAuditMarketGoods($goodsInfo)
    {
        $marketType = Db::name('market_type')
            ->where('id', Arr::get($goodsInfo, 'tid'))
            ->find() ?: [];
        $this->clearSetData()
            ->set('template', 'market_goods_check')
            ->set('url', 'index/Market/auditGoods?goodsId=' . Arr::get($goodsInfo, 'id'))
            ->set('first', '您好，您有新的二手商品提交需要审核。')
            ->set('keyword1', Arr::get($goodsInfo, 'name'))
            ->set('keyword2', Arr::get($goodsInfo, 'money') . '元')
            ->set('keyword3', Arr::get($marketType, 'name', '其他'))
            ->set('keyword4', Arr::get($goodsInfo, 'mobile'))
            ->set('keyword5', date('Y-m-d H:i'))
            ->set('remark', '请尽快审核发布，谢谢!');
        $checkJobUser = $this->getAgencyCheckUsers(
            Arr::get($goodsInfo, 'provinces_id'),
            Arr::get($goodsInfo, 'city_id'),
            Arr::get($goodsInfo, 'county_id'),
            2
        );
        if ($marketCheckUserArr = Arr::get($checkJobUser, 'marketCheckUsers', [])) {
            foreach ($marketCheckUserArr as $item) {
                $result = $this->set('toUser', Arr::get($item, 'wechat_openid'))
                    ->send();
            }
        }
        return true;
    }


    /**
     * 二手商品审核通过通知 -- 发送给发布人
     * @param $goodsInfo
     *
     * @return bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkMarketGoodsSuccess($goodsInfo)
    {
        $userInfo = Db::name('user')
            ->where('id', Arr::get($goodsInfo, 'publish_id', 0))
            ->field('id,wechat_openid,name,truename')
            ->find() ?: [];
        if ($userInfo) {
            $this->clearSetData()
                ->set('template', 'check_goods_success')
                ->set('url', 'index/Market/goodsDetail?id=2' . Arr::get($goodsInfo, 'id'))
                ->set('first', '您的发布的二手商品通过审核哦，快来看看吧！')
                ->set('keyword1', Arr::get($goodsInfo, 'name', ''))
                ->set('keyword2', date('Y-m-d H:i'))
                ->set('remark', '谢谢您的到来，我们竭力为您提供最优质的服务！')
                ->set('toUser', Arr::get($userInfo, 'wechat_openid'))
                ->send();
        }
        return true;
    }


    /**
     * 二手商品审核未通过  -- 发送给发布人 [审核不通过]
     * @param $goodsInfo
     * @param $message
     *
     * @return bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkMarketGoodsFalse($goodsInfo, $message)
    {
        $userInfo = Db::name('user')
            ->where('id', Arr::get($goodsInfo, 'publish_id', 0))
            ->field('id,wechat_openid,name,truename')
            ->find() ?: [];
        if ($userInfo) {
            $this->clearSetData()
                ->set('template', 'check_goods_false')
                ->set('url', 'index/Market/publishGoodsList?type=4')
                ->set('first', '您的商品没有通过审核哦，麻烦您尽快去修改。')
                ->set('keyword1', Arr::get($goodsInfo, 'name', ''))
                ->set('keyword2', $message)
                ->set('remark', '请您及时修改商品信息再提交审核，我们竭力为您提供最优质的服务！')
                ->set('toUser', Arr::get($userInfo, 'wechat_openid'))
                ->send();
        }
        return true;
    }






}
